package com.accum.client.view;

import java.util.List;

import com.accum.client.presenter.WordListPresenter;
import com.accum.shared.pojo.WordEntry;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HasHorizontalAlignment.HorizontalAlignmentConstant;

public class WordListView extends Composite implements WordListPresenter.Display {
	
	private final TextBox wordTextBox;
	private final Button addButton;
	private final FlexTable wordsTable;
	private TextBox fromDateField;
	private TextBox toDateField;

	public WordListView() {		
		
		HorizontalPanel mainPanel = new HorizontalPanel();
		mainPanel.setWidth("100%");
		mainPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		initWidget(mainPanel);
		
		
		/**Search Panel**/

		
		/***Word List Panel*/
		VerticalPanel wordsListPanel = new VerticalPanel();	
		mainPanel.add(wordsListPanel);	
		
			HorizontalPanel addNewItemPanel = new HorizontalPanel();
			wordsListPanel.add(addNewItemPanel);
				addNewItemPanel.add(new Label("New Entry: "));
				wordTextBox = new TextBox();
				addNewItemPanel.add(wordTextBox);
				addButton = new Button("Add");
				addButton.setStyleName("");
				addNewItemPanel.add(addButton);
				
			HorizontalPanel searchItemsPanel = new HorizontalPanel();
			wordsListPanel.add(searchItemsPanel);
				Label fromDateLabel = new Label("From");
				Label toDateLabel = new Label("To");
				fromDateField = new TextBox();
				toDateField = new TextBox();
				searchItemsPanel.add(fromDateLabel);
				searchItemsPanel.add(fromDateField);
				searchItemsPanel.add(toDateLabel);
				searchItemsPanel.add(toDateField);
				searchItemsPanel.add(new Button("Search"));
			
			wordsTable = new FlexTable();
			wordsTable.addStyleName("customers");
			wordsListPanel.add(wordsTable);
			wordsTable.setWidth("100%");
		
	}
	
	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public void setData(List<WordEntry> data) {
		wordsTable.removeAllRows();
	    
	    for (int i = 0; i < data.size(); ++i) {
	    	wordsTable.setText(i, 0, "" + data.get(i).getId());
	    	
	    	String entryName = data.get(i).getEntryName();
	    	Anchor hyperlink = new Anchor();
	    	hyperlink.setText(entryName);
	    	hyperlink.setHref("http://www.google.com/dictionary?hl=en&sl=en&tl=en&q=" + entryName);
	    	hyperlink.setTarget("_blank");
	    	
	    	wordsTable.setWidget(i, 1, hyperlink);
	    	
	    	wordsTable.setText(i, 2, data.get(i).getCreateDate().toLocaleString());
	    }
	}

	@Override
	public HasClickHandlers getAddButton() {
		return this.addButton;
	}

	@Override
	public String getEntryName() {
		return this.wordTextBox.getValue();
	}

	@Override
	public void clearEntryName() {
		this.wordTextBox.setValue(null);
	}

	public void addToList(String entryName) {
		int rowCount = wordsTable.getRowCount();
		wordsTable.setText(rowCount, 0, entryName);
	}

	@SuppressWarnings("deprecation")
	@Override
	public void addToList(WordEntry entry) {
		int rowCount = wordsTable.getRowCount();
		wordsTable.setText(rowCount, 0, "" + entry.getId());

		String entryName = entry.getEntryName();
    	Anchor hyperlink = new Anchor();
    	hyperlink.setText(entryName);
    	hyperlink.setHref("http://www.google.com/dictionary?hl=en&sl=en&tl=en&q=" + entryName);
    	hyperlink.setTarget("_blank");
    	
    	wordsTable.setWidget(rowCount, 1, hyperlink);
		
		wordsTable.setText(rowCount, 2, entry.getCreateDate().toLocaleString());
	}

}
